/*
 * @Description: default
 * @Autor: huoyou
 * @Date: 2022-01-12 10:28:23
 * @LastEditTime: 2022-01-12 11:17:38
 */

var http = require("http");
var fs = require("fs");
var path = require("path");
var url = require("url");
var qs = require("querystring");
var app = http.createServer();
var writeFile = require("./writeFile.js");

// 运行live-server，保持index.html热加载
const exec = require("child_process").exec;
exec("npm run live");

app.on("request", function (req, res) {
  var parseObj = url.parse(req.url, true);
  console.log(`🚀 ~ parseObj`, parseObj);
  var pathname = parseObj.pathname; //相当于无参数的url路径
  req.query = parseObj.query;
  if (pathname === "/api") {
    console.log(`🚀 ~ pathname`, pathname);
    console.log("req.query", req.query);
    const { envVal, typeVal, idVal } = req.query;
    writeFile(envVal, typeVal, idVal);
    res.writeHead(200);
    res.write("hello node.js!");
    res.end();
  }
});

app.listen(5000, function () {
  console.log("欢迎来到node-api");
});
